package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.BaseContext;
import com.chen.entity.AddressBook;
import com.chen.mapper.AddressBookMapper;
import com.chen.service.AddressBookService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Override
    public void add(AddressBook addressBook) {
        save(addressBook);
    }

    @Override
    public List<AddressBook> findlist(Long userId) {
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        return list(lqw);
    }

    @Override
    public void addressBookdefault(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(addressBook != null, AddressBook::getUserId, BaseContext.getCurrentId());
        lqw.set(AddressBook::getIsDefault, 0);
        update(lqw);
        assert addressBook != null;
        addressBook.setIsDefault(1);
        updateById(addressBook);
    }

    @Override
    public AddressBook get(Long id) {
        return getById(id);
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        updateById(addressBook);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            removeById(id);
        }
    }

    @Override
    public AddressBook getdefault() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getIsDefault,1);
        lqw.eq(AddressBook::getUserId,userId);
        return getOne(lqw);
    }

}
